package com.shenjianquan.cryptography.controller;

import com.shenjianquan.cryptography.handel.DESEncrypt;
import com.shenjianquan.cryptography.handel.DescryptCoder;
import com.shenjianquan.cryptography.handel.EncryptCoder;
import com.shenjianquan.cryptography.handel.RSAEncrypt;
import com.shenjianquan.cryptography.pojo.Encryption;
import com.shenjianquan.cryptography.pojo.Person;
import com.shenjianquan.cryptography.pojo.Result;
import com.shenjianquan.cryptography.utils.EncryptionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@CrossOrigin
public class viewsController {
    @GetMapping({"/test"})
    public String test(){
        return "test";
    }

    @GetMapping({"/data"})
    @ResponseBody
    public Result testData(Person person){
        System.out.println(person.getAge());
        System.out.println(person.getName());
        Result result = new Result();
        result.setCode(200);
        result.setData(null);
        return result;
    }



    @GetMapping({"/views","/"})
    public String view(){
        return "view";
    }

    @GetMapping("/cryptography")
    public String hasDateView(Encryption encryption, RedirectAttributes attributes, HttpSession session){
        if (!encryption.getPlaintext().isEmpty()){
            if (encryption.getButton().equals("RC4")){
                if (encryption.getSecretKey()==null){
                    encryption.setResult(null);
                    attributes.addFlashAttribute("encryption",encryption);
                    return "redirect:/views";
                }
                String result = EncryptionUtils.RC4(encryption.getPlaintext(), encryption.getSecretKey());
                encryption.setResult(result);
            }
            if (encryption.getButton().equals("RSA")){
                String result = null;

                if (encryption.getPlaintext().length()>30){
                    try {
                          result = RSAEncrypt.decrypt(encryption.getPlaintext(), RSAEncrypt.siyao);
//                        result = EncryptionUtils.RSADecryp(encryption.getPlaintext());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    encryption.setResult(result);
                    attributes.addFlashAttribute("encryption",encryption);
                    return "redirect:/views";
                }

                try {
                      result = RSAEncrypt.encrypt(encryption.getPlaintext(), RSAEncrypt.gongyao);
//                    result = EncryptionUtils.RSA(encryption.getPlaintext());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                encryption.setResult(result);
            }
            if (encryption.getButton().equals("DES")){
                String result = null;
                if (encryption.getSecretKey()==null){
                    attributes.addFlashAttribute("encryption",encryption);
                    return "redirect:/views";
                }
                String encryptString = EncryptCoder.encrypt(encryption.getPlaintext(),encryption.getSecretKey());
                encryption.setResult(encryptString);
                if (encryption.getPlaintext().length()>20){
                    String decrypt = DescryptCoder.decrypt(encryption.getPlaintext(), encryption.getSecretKey());
                    encryption.setResult(decrypt);
                }
            }
            if (encryption.getButton().equals("仿射")){
                //TODO 实现仿射逻辑

            }

            attributes.addFlashAttribute("encryption",encryption);
        }

        return "redirect:/views";

    }


    @GetMapping("/des")
    public String des(){
        return "des";
    }
}
